package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"path/filepath"
	"short-video/rpc/pkg"
)

func UploadFile(c *gin.Context) {
	file, err := c.FormFile("file")
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "获取文件失败",
		})
		return
	}
	if file.Size > 1024*1024*500 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "文件内存过大",
		})
		return
	}
	ext := filepath.Ext(file.Filename)
	if !(ext == ".mp4") {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "文件格式错误",
		})
		return
	}
	uploadFile, err := pkg.UploadFile(file, file.Filename)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "文件上传失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "文件上传成功过",
		"data": uploadFile,
	})
}
